#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/8/15 17:05
# @Author  : youfeng


class ProxyBase(object):
    # 最大生存时间控制 秒
    _PROXY_MAX_TTL = 180

    # # 过期时间字段
    # _EXPIRE_TIME_KEY = 'expire_time'
    #
    # # 代理字段
    # _PROXY_KEY = "proxy"

    def __int__(self, log):
        self.log = log
        # # 初始化redis
        # self._redis_temp_handler = redis.StrictRedis(host=TEMP_REDIS_CONF['host'],
        #                                             port=TEMP_REDIS_CONF['port'],
        #                                             db=TEMP_REDIS_CONF['db'],
        #                                             password=TEMP_REDIS_CONF['password'],
        #                                             max_connections=TEMP_REDIS_CONF['max_connections'])

    def get_proxy_list(self):
        """
        返回代理列表，实例: [{"proxy": "http://49.70.41.96:57112", "expire_time": 12313123123}]
        :return:
        """
        raise NotImplemented()

    def get_proxy_tag(self):
        """
        返回代理标签
        :return: __TAG__
        """
        raise NotImplemented()

    # # 从redis中获取所有的代理
    # def _get_proxy_list_from_redis(self, redis_key):
    #     proxy_list = []
    #     start_time = time.time()
    #
    #     try:
    #         proxy_temp_list = self._redis_temp_handler.lrange(redis_key, 0, -1)
    #         for proxy_str in proxy_temp_list:
    #             proxy_dict = json.loads(proxy_str)
    #             if not isinstance(proxy_dict, dict):
    #                 self.log.error("代理结构异常: proxy_str = {}".format(proxy_str))
    #                 continue
    #
    #             expire_time = proxy_dict.get(self._EXPIRE_TIME_KEY)
    #             if expire_time is None:
    #                 self.log.error("获取代理过期时间失败: {}".format(proxy_str))
    #                 continue
    #
    #             proxy_dict[self._EXPIRE_TIME_KEY] = Util.time_to_timestamp(expire_time)
    #             proxy_list.append(proxy_dict)
    #     except Exception as e:
    #         self.log.error("获取redis中代理异常: key = {}".format(redis_key))
    #         self.log.exception(e)
    #
    #     self.log.info("获取代理耗时: {} s".format(round(time.time() - start_time, 2)))
    #     return proxy_list
    #
    # # 存储代理到redis
    # def _set_proxy_list_to_redis(self, redis_key, proxy_list):
    #     start_time = time.time()
    #     pipe = self._redis_temp_handler.pipeline()
    #     for proxy_dict in proxy_list:
    #         try:
    #             # 再存储列表到redis
    #             expire_time = proxy_dict.get(self._EXPIRE_TIME_KEY)
    #             if expire_time is None:
    #                 self.log.error("当前代理没有设置过期时间: proxy_dict = {}".format(proxy_dict))
    #                 continue
    #
    #             proxy_dict[self._EXPIRE_TIME_KEY] = Util.timestamp_to_time(expire_time)
    #
    #             pipe.rpush(redis_key, json.dumps(proxy_dict))
    #         except Exception as e:
    #             self.log.error("存储代理到redis 异常: ")
    #             self.log.exception(e)
    #
    #     pipe.execute()
    #     self.log.info("存储代理耗时: num = {} {} s".format(len(proxy_list), round(time.time() - start_time, 2)))
    #
    # # 删除redis中已经存储的代理信息
    # def _delete_redis_proxy_list(self, redis_key):
    #     self._redis_temp_handler.delete(redis_key)
